/*
 * @Description: 全局状态仓库核心入口文件
 * @Author: LDM
 * @Date: 2021-09-03 17:23:53
 * @LastEditors: LDM
 * @LastEditTime: 2021-09-06 16:31:07
 */
import type { App } from 'vue'
import { InjectionKey } from 'vue'
import { createStore, useStore as getStore, Store } from 'vuex'
import { getVuexPlugins } from './plugins'
import { store as user } from './modules/user'
import { AllModuleTypes } from './interface'
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface RootState {
  name: string
}

const store = createStore<RootState>({
  plugins: getVuexPlugins(),
  state: {
    name: ''
  },
  modules: {
    user
  }
})
// 定义 injection key
const key: InjectionKey<Store<RootState>> = Symbol()
export function setupStore(app: App<Element>) {
  app.use(store, key)
}

export function useStore() {
  return getStore<AllModuleTypes>(key)
}
